﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

namespace DataPagination
{
    /// <summary>
    /// 分页_数字范围
    /// </summary>
    public class SplitType_NumberBound : PDataPage
    {
        public SplitType_NumberBound()
        {
            HtmlTemplate = @"
<span style='{0}'><a href={1}>上一页</a>&nbsp;</span>
<span style='{2}'><a href={3} alt='{4}'>首页</a>&nbsp;</span>
{5}
<span style='{6}'><a href={7} alt='{8}'>尾页</a>&nbsp;</span>
<span style='{9}'><a href={10}>下一页</a>&nbsp;</span>
";
        }

        #region 成员
        /// <summary>
        /// 两边数字范围
        /// </summary>
        protected const int sideBound = 5;

        /// <summary>
        /// 一个导航链接模板
        /// </summary>
        protected const string SingleHref = "<a href={0} alt='{1}'>{2}</a>&nbsp;";
        #endregion

        #region 需要重写的方法
        public override string GetSchema()
        {
            //运算好相关信息
            OperationInfo();
            return string.Format(HtmlTemplate,
                GetDisplay(IsFirstPage), //{0}
                GetUrl_PageIndex(CurrentPageIndex - 1),    //{1}
                GetDisplay(IsFirstPage), //{2}
                GetUrl_PageIndex(1),    //{3}
                1,  //{4}
                GetMiddleHref(),    //{5}
                GetDisplay(IsLastPage), //{6}
                GetUrl_PageIndex(PageCount),    //{7}
                PageCount,  //{8}
                GetDisplay(IsLastPage), //{9}
                GetUrl_PageIndex(CurrentPageIndex + 1)    //{10}
                );
        }

        protected string GetMiddleHref()
        {
            StringBuilder sb = new StringBuilder();
            String prv = string.Empty;
            String nex = string.Empty;
            int num_prv = CurrentPageIndex;
            int num_nex = CurrentPageIndex;
            string middle = string.Format(
                SingleHref, 
                GetUrl_PageIndex(CurrentPageIndex), 
                CurrentPageIndex, 
                "<font color=red>" + CurrentPageIndex.ToString() + "</font>");             
            
            //获取两边导航链接
            for (int i = 1; i <= sideBound; i++)
            {
                num_prv--;
                num_nex++;
                //左边
                if (num_prv > 0)
                {
                    prv = GetHref(GetUrl_PageIndex(num_prv), num_prv) + prv;
                    if (i == sideBound && num_prv > 1)
                    {
                        prv = "..." + prv;
                    }
                }
                //右边
                if (num_nex <= PageCount)
                {
                    nex += GetHref(GetUrl_PageIndex(num_nex), num_nex);
                    if (i == sideBound && num_nex < PageCount)
                    {
                        nex += "...";
                    }
                }
            }

            sb.Append(prv).Append(middle).Append(nex);
            
            return sb.ToString();
        }

        protected string GetHref(string _href, int _i)
        {
            return string.Format(SingleHref, _href, _i, _i);
        }

        protected string ShowRedHref(int mi)
        {
            return (mi == CurrentPageIndex) ? "<font color='red'>" + mi.ToString() + "</font>" : mi.ToString();
        }
        #endregion
    }    
}